package com.gzsxy.esjy.service.product.controller.api;

import com.gzsxy.esjy.service.base.DictionaryConstants;
import com.gzsxy.esjy.service.base.dto.response.*;
import com.gzsxy.esjy.service.product.entity.vo.ApiProductQueryVo;
import com.gzsxy.esjy.service.product.entity.vo.CategoryQueryVo;
import com.gzsxy.esjy.service.product.service.CategoryService;
import com.gzsxy.esjy.service.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.apache.coyote.http11.Constants.a;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 前台首页
 * @date 2022/2/7 11:29
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/api/index")
@Api("前台首页")
public class ApiIndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @ApiOperation("首页数据渲染")
    @GetMapping
    public DefaultResponse index(){
        CategoryQueryVo categoryQueryVo = new CategoryQueryVo();
        //TODO 获取所有的分类
        PageResult<CategoryRsp> pageResult = categoryService.selectPage(null, categoryQueryVo);
        List<CategoryRsp> records = pageResult.getRecords();
        //获取前15个分类
        if (records.size()>=15){
            records = records.subList(0, 15);
        }
        ApiIndexResponse apiIndexResponse = new ApiIndexResponse();
        //TODO 获取所有的商品信息
        //1热卖；2特价;3新品
        List<ProductRsp> productRsps = productService.getApiProductByQuery(DictionaryConstants.NUMBER_0);
        List<ProductRsp> productRsps1 = productService.getApiProductByQuery(DictionaryConstants.NUMBER_1);
        List<ProductRsp> productRsps2 = productService.getApiProductByQuery(DictionaryConstants.NUMBER_2);

        apiIndexResponse.setCategoryRsp(records);
        apiIndexResponse.setProductBySale(productRsps);
        apiIndexResponse.setProductByPrice(productRsps1);
        apiIndexResponse.setProductByCreateTime(productRsps2);
        return DefaultResponse.ok(apiIndexResponse);
    }
}
